package com.ksd.common.core.result;


import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.google.common.collect.Maps;
import com.ksd.common.core.response.ResponseEnum;
import lombok.Getter;

import java.util.Map;
import java.util.Objects;

/**
 * @description:
 * @author: xuke
 * @time: 2021/6/23 21:47
 */

// json序列化的时候，如果属性是null，key也就一起消失
@JsonInclude(JsonInclude.Include.NON_NULL)
@Getter
public class R<T> implements java.io.Serializable {
    // 返回的编号
    private Integer code;
    // 返回的数据,数据类型N中，
    private T data;
    // 返回额外的数据
    private Map<String, Object> result;
    // 返回的信息
    private String message;

    // 调用过程中，保持一种调用。直接用类去调用。
    private R() {

    }

    private R(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    private R(T data, Integer code, String message) {
        this.data = data;
        this.code = code;
        this.message = message;
    }

    private R(T data, Integer code, String message, Map<String, Object> result) {
        this.data = data;
        this.code = code;
        this.message = message;
        this.result = result;
    }

    /**
     * @return com.kuangstudy.common.R
     * @Author xuke
     * @Description 成功返回
     * @Date 21:55 2021/6/23
     * @Param []
     **/
    public static <T> R<T> success(T data, String message) {
        return new R<T>(data, ResponseEnum.SUCCESS.getCode(), message);
    }

    public static <T> R<T> success(T data) {
        return new R<T>(data, ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
    }

    public static R success() {
        return new R(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
    }

    /**
     * @return com.kuangstudy.common.R
     * @Author xuke
     * @Description 成功返回
     * @Date 21:55 2021/6/23
     * @Param []
     **/
    public static <T> R<T> success(String message) {
        return new R<T>(ResponseEnum.SUCCESS.getCode(), message);
    }

    /**
     * @return com.kuangstudy.common.R
     * @Author xuke
     * @data 成功返回
     * @Date 21:55 2021/6/23
     * @Param []
     **/
    public static <T> R<T> data(T data) {
        return new R<T>(data, ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
    }

    /**
     * @return com.kuangstudy.common.R
     * @Author xuke
     * @Description 成功返回
     * @Date 21:55 2021/6/23
     * @Param []
     **/
    public <T> R<T> putMap(String key, Object value) {
        if(Objects.isNull(this.result)) {
            this.result = Maps.newHashMap();
        }
        this.result.put(key, value);
        return (R<T>) this;
    }

    public static void main(String[] args) {
        R<Object> objectR = R.data("ok").putMap("name", "zhangsan").putMap("age","30");
        System.out.println(JSONUtil.toJsonStr(objectR));
    }

    /**
     * @return com.kuangstudy.common.R
     * @Author xuke
     * @Description 成功返回
     * @Date 21:55 2021/6/23
     * @Param []
     **/
    public static <T> R<T> fail() {
        return new R<T>(ResponseEnum.ERROR.getCode(), ResponseEnum.ERROR.getMsg());
    }

    /**
     * @return com.kuangstudy.common.R
     * @Author xuke
     * @Description
     * @Date 22:03 2021/6/23
     * @Param [code 失败的状态, message 失败的原因]
     **/
    public static <T> R<T> fail(Integer code, String message) {
        return new R<T>(code, message);
    }

    /**
     * @return com.kuangstudy.common.R
     * @Author xuke
     * @Description
     * @Date 22:03 2021/6/23
     * @Param [code 失败的状态, message 失败的原因]
     **/
    public static <T> R<T> fail(ResponseEnum responseEnum) {
        return new R<T>(responseEnum.getCode(), responseEnum.getMsg());
    }


}

